package com.cms.manage.util;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.fileupload.FileItem;
import org.springframework.web.multipart.MultipartFile;

import com.cms.manage.common.ConmonConfig;
import com.cms.manage.common.SysCode;
import com.google.common.collect.Range;

public class ImageUtil {
	
	  /**
     * 判断是否是图片
     * @param obj
     * @return
     */
    public static boolean notImageFormat(String suffix) {
        // 文件名称为空的场合
        if (StringUtil.isNullOrEmpty(suffix)) {
            return false;
        }
        // 声明图片后缀名数组
     //   String imgeArray[] = { "bmp","gif", "jpe","jpeg","jpg","png","tiff"};
        String imgeArray[] = { "jpg","jpeg", "gif","png"};
        // 遍历名称数组
        for (int i = 0; i < imgeArray.length; i++) {
            // 判断单个类型文件的场合
            if (imgeArray[i].equals(suffix.toLowerCase())) {
                return true;
            }
        }
        return false;
    }
    /**
     * 上传图片
     * @param data 
     * @param obj
     * @return
     * @throws IOException 
     */
	public static Map<String, String> uploadImage(List<MultipartFile> fileList, Map<String, String> data){	
		String imageType =data.get("type");
		 Map<String, String> rs= new HashMap<>(); 
			if (fileList.size()>0){
					// 这里上传多张图片
				MultipartFile file = fileList.get(0);
				  String suffix ="";
					if (file != null) {
						// 取得当前上传文件的文件名称
						// 获取文件路径
						String filePath = file.getOriginalFilename();
						// 获取文件后缀名
						 suffix = filePath.substring(filePath.lastIndexOf(".") + 1);			
							if (!ImageUtil.notImageFormat(suffix)) {
								rs.put("error", "pu_type_err");
								return rs;
							}
							//文件名字
							String filename = "_"+System.currentTimeMillis()+suffix;
							int MAX_POST_SIZE = 2 * 1024 * 1024;// 限制2M
							if (file.getSize() > MAX_POST_SIZE) {//pu_type_err
								rs.put("error", "pu_capacity_err");
								return rs;
							}
						}
						String result ="";
						try {
							BufferedImage sourceImg =ImageIO.read(file.getInputStream());
							if("photo".equals(imageType)){
								if(sourceImg.getHeight()<200||sourceImg.getHeight()<200){
									rs.put("error", "u_up_small");
									return rs;
								}
							}
							if("ico".equals(imageType)){
								if(sourceImg.getHeight()>=200||sourceImg.getHeight()>=200){
									rs.put("error", "u_upl_size_err");	
									return rs;
								}
							}							
							result = OSSUploadUtil.uploadFile(file.getInputStream(),suffix);
							rs.put("success",result);
							return rs;
						} catch (IOException e) {
							rs.put("error", "bad_upload_io_error");
							return rs;
						}							
					}  
			return rs;
	}	
	 /**
     * 上传图片
     * @param data 
     * @param obj
     * @return
	 * @throws Exception 
     * @throws IOException 
     */
	public static Map<String, String> uploadImageByOne(MultipartFile file, Map<String, String> data){	
		 Map<String, String> rs= new HashMap<>(); 	
					// 这里上传多张图片		
				  String suffix ="";
					if (file != null) {
						// 取得当前上传文件的文件名称
						// 获取文件路径
						String filePath = file.getOriginalFilename();
						// 获取文件后缀名
						 suffix = filePath.substring(filePath.lastIndexOf(".") + 1);			
							if (!ImageUtil.notImageFormat(suffix)) {								
								rs.put("error", "pu_type_err");
								return rs;
							}
							//文件名字
							String filename = "_"+System.currentTimeMillis()+suffix;
							int MAX_POST_SIZE = 2 * 1024 * 1024;// 限制2M
							if (file.getSize() > MAX_POST_SIZE) {//pu_type_err
								rs.put("error", "pu_capacity_err");
								return rs;
							}
						}else{
							rs.put("error", "null");
							return rs;
						}
						String baseResult ="";
						try {
							baseResult = OSSUploadUtil.uploadFile(file.getInputStream(),suffix);						
							rs.put("imgUrl",baseResult);
							rs.put("success","success");
							return rs;
						} catch (IOException e) {
							rs.put("error", "bad_upload_io_error");
							return rs;
					}															
	}
	
	/**
     * 上传图片到本地
     * @param data 
     * @param obj
     * @return
	 * @throws Exception 
     * @throws IOException 
     */
	public static Map<String, Object> uploadImageByOneBase(MultipartFile file, Map<String, String> data){	
		 Map<String, Object> rs = ConmonConfig.getResultMap();	
					// 这里上传多张图片		
				  String suffix ="";
				  String filename="";
					if (file != null) {
						// 取得当前上传文件的文件名称
						// 获取文件路径
						String filePath = file.getOriginalFilename();
						// 获取文件后缀名
						 suffix = filePath.substring(filePath.lastIndexOf(".") + 1);			
							if (!ImageUtil.notImageFormat(suffix)) {								
								rs.put("error", "pu_type_err");
								return rs;
							}
							//文件名字
							 filename = UUID.randomUUID()+"_"+System.currentTimeMillis()+"."+suffix;
							int MAX_POST_SIZE = 2 * 1024 * 1024;// 限制2M
							if (file.getSize() > MAX_POST_SIZE) {//pu_type_err
								rs.put("error", "pu_capacity_err");
								return rs;
							}
						}else{
							rs.put("error", "null");
							return rs;
						}
						String baseResult ="";
						try {
						    baseResult = UploadBaseFileUtil.writeFileByData(filename,file,Constant.BACK_IMG_PATH);											
							rs.put("data",baseResult);
							return rs;
						} catch (IOException e) {
							rs.put("code",SysCode.error);
							rs.put("message", "上传失败！"); 
							return rs;
					}															
	}
	
	
	 /**
     * 上传缩略图
     * @param data 
     * @param obj
     * @return
	 * @throws Exception 
     * @throws IOException 
     */
	public static Map<String, String> uploadImageByOne(MultipartFile file, Map<String, String> data,int formatWideth, int formatHeight){	
		 Map<String, String> rs= new HashMap<>(); 	
					// 这里上传多张图片		
				  String suffix ="";
					if (file != null) {
						// 取得当前上传文件的文件名称
						// 获取文件路径
						String filePath = file.getOriginalFilename();
						// 获取文件后缀名
						 suffix = filePath.substring(filePath.lastIndexOf(".") + 1);			
							if (!ImageUtil.notImageFormat(suffix)) {								
								rs.put("error", "pu_type_err");
								return rs;
							}
							//文件名字
							String filename = "_"+System.currentTimeMillis()+suffix;
							int MAX_POST_SIZE = 2 * 1024 * 1024;// 限制2M
							if (file.getSize() > MAX_POST_SIZE) {//pu_type_err
								rs.put("error", "pu_capacity_err");
								return rs;
							}
						}else{
							rs.put("error", "null");
							return rs;
						}
						String baseResult ="";
						String smallResult ="";
						try {
							BufferedImage sourceImg =ImageIO.read(file.getInputStream());	
							if(sourceImg.getHeight()>formatWideth&&sourceImg.getHeight()>formatHeight){
								ImageUtils imageUtils = new ImageUtils();
								smallResult = imageUtils.saveImageAsSmall(file, formatWideth, formatHeight, suffix);
								//生成缩略图
							}else{
								smallResult =OSSUploadUtil.uploadFile(file.getInputStream(),suffix);
							}
							rs.put("imgUrl",smallResult);
							rs.put("success","success");
							return rs;
						} catch (IOException e) {
							rs.put("error", "bad_upload_io_error");
							return rs;
					}															
	}
	public static void main(String[] args) {
		
        int a =1;
        if(a==1){
        	System.out.println("111");
        }else if(a==2){
        	System.out.println("222");
        }else if(a!=2){
        	System.out.println("333");
        }
	    
		System.out.println(DigestUtils.md5Hex("52649"+"25"+"0"+"winston"));
		
	
	}
}
